package com.karolak.web.saf.command;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.karolak.service.Service;
import com.karolak.web.saf.action.ModelAction;
import com.opensymphony.xwork2.Action;

public class EditCommand implements ActionCommand {

	protected Log log = LogFactory.getLog(getClass().getName());

	public <T, Id, S extends Service<T, Id>> void prepare(ModelAction<T, Id, S> action) {
		Id id = action.getModelId();
		if (id == null) {
			String msg = action.getText("invalid.id", new String[] { "" + id });
			action.addActionError(msg);
			if (log.isWarnEnabled()) {
				log.warn(msg);
			}
			return;
		}

		T entity = action.getService().findById(id);
		action.setModel(entity);
		if (action.getModel() == null) {
			String msg = action.getText("entity.does.not.exist", new String[] { "" + id });
			action.addActionError(msg);
			if (log.isWarnEnabled()) {
				log.warn(msg);
			}
			return;
		}
	}

	public <T, Id, S extends Service<T, Id>> void validate(ModelAction<T, Id, S> action) {

	}

	public <T, Id, S extends Service<T, Id>> String execute(ModelAction<T, Id, S> action) {
		return Action.INPUT;
	}
}
